package com.dk.service.goods;

import com.alibaba.dubbo.config.annotation.Service;
import com.dk.mapper.item.TbItemMapper;
import com.dk.pojo.item.TbItem;
import com.dk.pojo.item.TbItemExample;
import com.dk.pojo.order.TbOrderItem;
import com.dk.service.item.ItemService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Service
@Component
public class ItemServiceImpl implements ItemService {
    @Resource
    private TbItemMapper tbItemMapper;

    @Override
    public TbItem getItemById(Long itemId) {
        return tbItemMapper.selectByPrimaryKey(itemId);
    }

    @Override
    public int setStockCount(List<TbOrderItem> orderItemList) {
        int i = 0;
        for (TbOrderItem orderItem : orderItemList) {
            TbItem item = tbItemMapper.selectByPrimaryKey(orderItem.getItemId());
            item.setStockCount(item.getStockCount() - orderItem.getNum());
            int j = tbItemMapper.updateByPrimaryKeySelective(item);
            i = j+i;
        }
        return i;
    }

    @Override
    public List<TbItem> getItemByGoodsId(Long id) {
        TbItemExample tbItemExample = new TbItemExample();
        TbItemExample.Criteria criteria = tbItemExample.createCriteria();
        criteria.andGoodsIdEqualTo(id);
        return tbItemMapper.selectByExample(tbItemExample);
    }
}
